package dom.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.mysql.jdbc.PreparedStatement;

import dom.bean.User;



public class Reg implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest httpServletRequest=(HttpServletRequest)request;
		String psw=request.getParameter("regpsw");
		String account=request.getParameter("regtext");
		int id=Integer.parseInt(request.getParameter("regid")) ;

		
		com.mysql.jdbc.Connection conn=Connections.getConnection();
		PreparedStatement pstm=null;
		ResultSet rs=null;
		String SQL="select * from manager where id=?";
		try {
			pstm =(PreparedStatement) conn.prepareStatement(SQL);
			pstm.setInt(1,id);
			rs= pstm.executeQuery();
			if(rs.next()){
				request.setAttribute("regError", "<center><h1><b>账号已存在，请直接登陆<b></h1></center>");
				request.getRequestDispatcher("../Login.jsp").forward(httpServletRequest, response);
			}else{
				chain.doFilter(request, response);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Connections.closeDB(conn, pstm, rs);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
